package com.pj.point.util.alert;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.pj.point.R;
import com.pj.point.util.view.MyDialog;

public class AlertUtil {
    public static void show(Activity activity, String title, String msg, AlertThen then) {
        MyDialog dialog = new MyDialog(activity);
        View dialogView = dialog.createView(activity, R.layout.alert);
        TextView alert_title = dialogView.findViewById(R.id.alertTitle);
        alert_title.setText(title);
        TextView message = dialogView.findViewById(R.id.message);
        message.setText(msg);
        EditText editText = dialogView.findViewById(R.id.edit_password);
        message.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
        dialog.show();
        dialogView.findViewById(R.id.alert_ok).setOnClickListener(v -> {
            then.then(true);
            dialog.dismiss();
        });
        dialogView.findViewById(R.id.alert_cancel).setOnClickListener(v -> {
            then.then(false);
            dialog.dismiss();
        });
    }


    public static void show(Activity activity, String title, EditThen then) {
        MyDialog dialog = new MyDialog(activity);
        View dialogView = dialog.createView(activity, R.layout.alert);
        TextView alert_title = dialogView.findViewById(R.id.alertTitle);
        alert_title.setText(title);
        TextView message = dialogView.findViewById(R.id.message);
        message.setVisibility(View.GONE);
        EditText editText = dialogView.findViewById(R.id.edit_password);
        editText.setVisibility(View.VISIBLE);
        dialog.show();
        Button  ok = dialogView.findViewById(R.id.alert_ok);
        ok.setOnClickListener(v -> {
            then.then(editText.getText().toString().trim());
            dialog.dismiss();
        });
        Button cancel = dialogView.findViewById(R.id.alert_cancel);
        cancel.setOnClickListener(v -> dialog.dismiss());
    }

    public static void show(Activity activity, String title, String msg, AlertThen then, boolean b) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(title)
                .setMessage(msg)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    dialogInterface.dismiss();
                    then.then(true);
                }).setCancelable(false);
        builder.show();
    }

}
